package com.gxuwz.endrskyweather.viewmodel;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.gxuwz.endrskyweather.model.AlarmList;
import com.gxuwz.endrskyweather.model.Day;
import com.gxuwz.endrskyweather.model.LifeIndex;
import com.gxuwz.endrskyweather.model.Night;
import com.gxuwz.endrskyweather.model.Now;
import com.gxuwz.endrskyweather.apiclient.BaiduBceApiClient24h;
import com.gxuwz.endrskyweather.apiclient.IApiClient;

import org.json.JSONException;

import java.io.IOException;

public class CityWeatherViewModel extends AndroidViewModel {

    private IApiClient client;
    private MutableLiveData<Now> weatherNow;        // 当前天气数据
    private MutableLiveData<AlarmList> alarmList;   // 预警信息
    private String aqiLevel;                        // 空气质量评级
    private MutableLiveData<LifeIndex> lifeIndex;   // 生活指数
    private Day day;
    private Night night;

    private String jsonString;


    public CityWeatherViewModel(@NonNull Application application) {
        super(application);
        weatherNow = new MutableLiveData<>();
        aqiLevel = "";
        alarmList = new MutableLiveData<>();
        lifeIndex = new MutableLiveData<>();
        day = new Day();
        night = new Night();
        //client = new ApiClient();
        client = new BaiduBceApiClient24h(application.getApplicationContext());
        jsonString = null;
    }

    public MutableLiveData<Now> getWeatherNow() {
        return weatherNow;
    }
    public String getAqiLevel() {
        return aqiLevel;
    }
    public Day getDay() { return day; }
    public Night getNight() { return night; }
    public MutableLiveData<LifeIndex> getLifeIndex() {
        return lifeIndex;
    }
    public MutableLiveData<AlarmList> getAlarmList() {
        return alarmList;
    }

    public void loadWeatherNow(String cityCode) {
        new Thread(() -> {
            try {
                // loadWeather 作为加载启动项目，每次调用必须更新缓存的 json
                jsonString = client.httpGetJsonString(cityCode);
                Now now = new BaiduBceApiClient24h(jsonString).getNowWeather(cityCode);
                loadDay(cityCode);
                loadNight(cityCode);
                loadAlarmList(cityCode);
                loadLifeIndex(cityCode);
                aqiLevel = doAqiLevel(Integer.parseInt(now.getAqi()));
                weatherNow.postValue(now);
                Log.d("CityWeatherViewModel", "loadWeatherNow: " + now);
            } catch (Exception ex) {
                Log.e("CityWeatherViewModel", "loadWeatherNow: ", ex);
            }
        }).start();
    }

    public void loadLifeIndex(String cityCode) {
        try {
            if (jsonString == null) jsonString = client.httpGetJsonString(cityCode);
            LifeIndex index = new BaiduBceApiClient24h(jsonString).getLifeIndex(cityCode);
            lifeIndex.postValue(index);
        } catch (Exception ex) {
            Log.e("CityWeatherViewModel", "loadLifeIndex: ", ex);
        }
    }

    public void loadAlarmList(String cityCode) {
        try {
            if (jsonString == null) jsonString = client.httpGetJsonString(cityCode);
            AlarmList alarm = new BaiduBceApiClient24h(jsonString).getAlarmList(cityCode);
            alarmList.postValue(alarm);
        } catch (Exception ex) {
            Log.e("CityWeatherViewModel", "loadAlarmList: ", ex);
        }
    }

    public void loadDay(String cityCode) throws IOException, JSONException {
        if (jsonString == null) jsonString = client.httpGetJsonString(cityCode);
        Day day = new BaiduBceApiClient24h(jsonString).getDayWeather(cityCode);
        this.day = day;
    }

    public void loadNight(String cityCode) throws IOException, JSONException {
        if (jsonString == null) jsonString = client.httpGetJsonString(cityCode);
        Night night = new BaiduBceApiClient24h(jsonString).getNightWeather(cityCode);
        this.night = night;
    }

    private String doAqiLevel(Integer aqi) {
        if (aqi == null) {
            return "未知";
        }
        if (aqi <= 50) {
            return "Ⅰ级（优）";
        } else if (aqi <= 100) {
            return "Ⅱ级（良）";
        } else if (aqi <= 150) {
            return "Ⅲ级（轻度污染）";
        } else if (aqi <= 200) {
            return "Ⅳ级（中度污染）";
        } else if (aqi <= 300) {
            return "Ⅴ级（重度污染）";
        } else {
            return "Ⅵ级（严重污染）";
        }
    }

}
